{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "load glove finished\n"
     ]
    }
   ],
   "source": [
    "from sentiment import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_model = BertSenti(bert_dir=\"../bertModel/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_model.load_model(\"/home/hadoop/Downloads/airline_bert_senti.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import SentimentPlot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from SubCRD.twitterloader import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "    dir = \"../pheme-rnr-dataset\"\n",
    "    events = [os.path.join(dir, item) for item in os.listdir(dir)]\n",
    "    events = [\"../\"+e for e in events if os.path.isdir(e)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['../../pheme-rnr-dataset/germanwings-crash',\n",
       " '../../pheme-rnr-dataset/ottawashooting',\n",
       " '../../pheme-rnr-dataset/sydneysiege',\n",
       " '../../pheme-rnr-dataset/ferguson',\n",
       " '../../pheme-rnr-dataset/charliehebdo']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "events"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "te = TwitterSet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "load len:  5802\n"
     ]
    }
   ],
   "source": [
    "te.load_data_fast(data_prefix=\"./data/twitter\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt_seqs = [[\" \".join(txt) for txt in te.data[ID]['text']] for ID in te.data_ID]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "class SentimentPlot(object):\n",
    "    def __init__(self, sentiment_model, bins=20, xlabel=\"\", ylabel=\"\", title=\"\"):\n",
    "        self.s_model = sentiment_model\n",
    "        plt.xlabel(xlabel)\n",
    "        plt.ylabel(ylabel)\n",
    "        plt.title(title)\n",
    "        plt.xlim(0, 1)\n",
    "        plt.ylim(0, 10)\n",
    "        self.bins = bins\n",
    "\n",
    "    def textseq2sentiseq(self, text_seqs):\n",
    "        senti_seqs = []\n",
    "        for seq in tqdm(text_seqs):\n",
    "            with torch.no_grad():\n",
    "                senti_scores = self.s_model(seq)\n",
    "            senti_seqs.append(senti_scores)\n",
    "        return senti_seqs\n",
    "\n",
    "    def senti_shift(self, senti_seqs, start=0, end=1, legend=\"\"):\n",
    "        colors = [(1, 0, 0), (1, 1, 0), (0, 1, 0,), (0, 0, 1)]\n",
    "        pos_head = [senti[start] for senti in senti_seqs]\n",
    "        pos_tail = [senti[end] for senti in senti_seqs]\n",
    "        sns.distplot(pos_head, bins=self.bins, rug=False, kde=True, hist=True, norm_hist=True, label=\"source\",\n",
    "                     hist_kws={\"histtype\": \"step\", \"linewidth\": 2,\n",
    "                               \"alpha\": 1}, kde_kws={\"color\": colors[0], \"lw\": 0, \"label\": \"\"})\n",
    "        sns.distplot(pos_tail, bins=self.bins, rug=False, kde=True, hist=True, norm_hist=True, label=\"tail\",\n",
    "                     hist_kws={\"histtype\": \"step\", \"linewidth\": 2,\n",
    "                               \"alpha\": 1}, kde_kws={\"color\": colors[0], \"lw\": 0, \"label\": \"\"})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAD8CAYAAAB9y7/cAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAALwElEQVR4nO3cf4zkd13H8eeLLhV7VKh2JdhWryaltal/tG60SILIVVOraf+QmGtSbUnjJRgRkWhq/KNG/9GoRE2IuELFH1jASuCCP5CUNo2GXtxrEds7kVpqOTi8xR/VSKRU3v4xE7a5cN3Z+X53Znnf85Fsbmb3OzPvfLL7vO98Z+abqkKS1M/zlj2AJGl3GHhJasrAS1JTBl6SmjLwktSUgZekprYNfJK7kpxK8sizvvf1ST6U5BPTfy/Y3TElSTs1yx78O4DrT/veHcC9VXUZcO/0uiRpD8ksH3RKsh/4QFVdNb3+ceBVVXUyyUuB+6vq8t0cVJK0Mytz3u4lVXVyevmzwEvOtGGSQ8AhgH379n3HFVdcMedDStLZ6ejRo5+rqtWd3m7ewH9ZVVWSMz4NqKp1YB1gbW2tNjY2hj6kJJ1VkvzLPLeb9100/zo9NMP031Nz3o8kaZfMG/jDwK3Ty7cC7x9nHEnSWGZ5m+TdwEeAy5OcSHI78CvA9yX5BHDd9LokaQ/Z9hh8Vd18hh8dGHkWSdKI/CSrJDVl4CWpKQMvSU0ZeElqysBLUlMGXpKaMvCS1JSBl6SmDLwkNWXgJakpAy9JTRl4SWrKwEtSUwZekpoy8JLUlIGXpKYMvCQ1ZeAlqSkDL0lNGXhJasrAS1JTBl6SmjLwktSUgZekpgy8JDVl4CWpKQMv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SNzJ5wfW2hjuDACS5m8l/7BdOX3O4E3g+QFW9lclrEDcAjwGfB1677X02XStJOuv5SVZJasrAS1JTBl6SmjLwktSUgZekpgy8JDVl4CWpqf8HZj6enVHNQqUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "spt = SentimentPlot(s_model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5802/5802 [02:36<00:00, 37.00it/s]\n"
     ]
    }
   ],
   "source": [
    "senti_seqs = spt.textseq2sentiseq(txt_seqs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub_scores = [sco[:, 1].tolist() for sco in senti_seqs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "rumor_scores = [sco for idx, sco in enumerate(sub_scores) if te.data_y[idx][0] == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "nonrumor_score = [sco for idx, sco in enumerate(sub_scores) if te.data_y[idx][1] == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1972, 3830)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(rumor_scores), len(nonrumor_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.2966979444026947,\n",
       "  0.3113813102245331,\n",
       "  0.3243025243282318,\n",
       "  0.30146104097366333,\n",
       "  0.32919740676879883,\n",
       "  0.3033313453197479,\n",
       "  0.3199504315853119,\n",
       "  0.3311866223812103,\n",
       "  0.33469513058662415,\n",
       "  0.31309977173805237,\n",
       "  0.3008662164211273,\n",
       "  0.30914944410324097,\n",
       "  0.31953856348991394,\n",
       "  0.3295799791812897,\n",
       "  0.3125731348991394,\n",
       "  0.29761773347854614,\n",
       "  0.3032056391239166],\n",
       " [0.33949676156044006],\n",
       " [0.3022637367248535,\n",
       "  0.32536178827285767,\n",
       "  0.3253253996372223,\n",
       "  0.28324416279792786,\n",
       "  0.2964761257171631,\n",
       "  0.31656453013420105]]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nonrumor_score[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.3390452563762665,\n",
       "  0.2927418351173401,\n",
       "  0.3043789863586426,\n",
       "  0.31030961871147156,\n",
       "  0.31043240427970886,\n",
       "  0.3267371654510498,\n",
       "  0.28501778841018677,\n",
       "  0.3172459006309509,\n",
       "  0.26791754364967346,\n",
       "  0.30526769161224365,\n",
       "  0.3074275255203247,\n",
       "  0.3334105312824249,\n",
       "  0.3488309979438782,\n",
       "  0.28102943301200867,\n",
       "  0.31738749146461487,\n",
       "  0.2812351882457733,\n",
       "  0.3124338388442993,\n",
       "  0.33308759331703186,\n",
       "  0.30889201164245605,\n",
       "  0.28056758642196655,\n",
       "  0.3293250501155853,\n",
       "  0.25704827904701233,\n",
       "  0.3133316934108734,\n",
       "  0.29683175683021545,\n",
       "  0.290147066116333,\n",
       "  0.28092658519744873],\n",
       " [0.3281976282596588,\n",
       "  0.3281976282596588,\n",
       "  0.32178089022636414,\n",
       "  0.36151400208473206,\n",
       "  0.33334192633628845,\n",
       "  0.3350825905799866,\n",
       "  0.3257327675819397,\n",
       "  0.32626619935035706,\n",
       "  0.2970587909221649,\n",
       "  0.31098034977912903,\n",
       "  0.3212854266166687,\n",
       "  0.321469783782959,\n",
       "  0.34980788826942444,\n",
       "  0.30259478092193604,\n",
       "  0.3022298216819763,\n",
       "  0.3264279365539551,\n",
       "  0.33804166316986084,\n",
       "  0.32175830006599426,\n",
       "  0.3122987449169159],\n",
       " [0.34787601232528687,\n",
       "  0.31315597891807556,\n",
       "  0.32350191473960876,\n",
       "  0.3389274477958679,\n",
       "  0.35426729917526245,\n",
       "  0.31547126173973083,\n",
       "  0.3040298819541931,\n",
       "  0.3185904920101166,\n",
       "  0.32359778881073,\n",
       "  0.29374560713768005,\n",
       "  0.33265259861946106,\n",
       "  0.3271614611148834,\n",
       "  0.3429965078830719,\n",
       "  0.3357093632221222,\n",
       "  0.34625715017318726,\n",
       "  0.31691357493400574,\n",
       "  0.2910669445991516]]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rumor_scores[:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.autograd import Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class GradReverse(Function):\n",
    "    @staticmethod\n",
    "    def forward(ctx, x):\n",
    "        return x.view_as(x)\n",
    "    \n",
    "    @staticmethod\n",
    "    def backward(ctx, grads):\n",
    "        return grads.neg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def grad_reverse(x):\n",
    "    return GradReverse.apply(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "class GradReverseLinear(nn.Module):\n",
    "    def __init__(self, l):\n",
    "        super(GradReverseLinear, self).__init__()\n",
    "        self.linear = l\n",
    "    def forward(self, x):\n",
    "        t = grad_reverse(x)\n",
    "        return self.linear(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "l1 = nn.Linear(10, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "l2 = nn.Linear(10, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "g_l2 = GradReverseLinear(l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss_fn = nn.MSELoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loss1(x, y):\n",
    "    return loss_fn(g_l2(l1(x)), y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loss2(x, y):\n",
    "    return loss_fn(l2(l1(x)), y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = torch.randn(10, 10)\n",
    "y = torch.randn(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/hadoop/.conda/envs/torch_B/lib/python3.6/site-packages/torch/nn/modules/loss.py:431: UserWarning: Using a target size (torch.Size([10])) that is different to the input size (torch.Size([10, 10])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.\n",
      "  return F.mse_loss(input, target, reduction=self.reduction)\n"
     ]
    }
   ],
   "source": [
    "n_loss = loss2(x, y)\n",
    "n_loss.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[tensor([[-2.7389e-02,  1.1402e-02, -1.3026e-02, -1.9048e-02,  2.6334e-02,\n",
       "          -4.4004e-02, -1.1785e-02, -3.4722e-02,  2.0086e-02, -3.4167e-02],\n",
       "         [-9.5504e-02,  7.0130e-02, -1.5741e-02, -1.0722e-01,  4.3244e-02,\n",
       "          -1.4641e-01,  1.1110e-02, -1.0949e-01,  1.1689e-01, -7.9042e-02],\n",
       "         [-1.1131e-02,  1.6760e-02, -8.1567e-03,  2.1239e-02,  1.5258e-02,\n",
       "          -2.7346e-02,  2.2119e-02, -2.6933e-02,  2.1116e-02,  8.4700e-05],\n",
       "         [-8.4151e-03,  1.3514e-02,  6.6931e-03, -3.5969e-02, -1.5061e-02,\n",
       "          -1.4408e-02,  3.7014e-03, -1.4785e-02,  2.3280e-02,  8.8669e-03],\n",
       "         [ 5.0170e-02, -1.8824e-02,  6.6798e-03,  7.7978e-02,  3.5121e-03,\n",
       "           3.5753e-02,  6.5429e-03,  3.0482e-02, -5.1790e-02,  1.4147e-02],\n",
       "         [-2.6445e-02,  4.6301e-02,  4.2772e-02, -8.4189e-02, -3.3793e-02,\n",
       "          -7.6141e-02,  1.6340e-02, -4.9749e-02,  6.0687e-02, -5.9759e-03],\n",
       "         [-2.7532e-02,  1.7192e-02,  1.7555e-02, -6.6511e-02, -2.3261e-02,\n",
       "          -3.1535e-02, -2.2324e-03, -2.0739e-02,  3.4416e-02, -2.8350e-03],\n",
       "         [-1.0294e-01,  1.0090e-01,  3.4701e-02, -1.3869e-01, -2.0929e-04,\n",
       "          -1.9544e-01,  4.0068e-02, -1.3714e-01,  1.4739e-01, -6.2956e-02],\n",
       "         [ 4.1102e-02, -4.1929e-02,  4.8118e-03,  1.1362e-02, -2.2476e-02,\n",
       "           7.5625e-02, -3.4561e-02,  5.8165e-02, -5.9864e-02,  2.4102e-02],\n",
       "         [-2.3423e-02,  2.5135e-02,  3.3604e-02, -9.1833e-02, -4.1923e-02,\n",
       "          -2.3969e-02,  7.2757e-03, -8.3311e-03,  4.7152e-02,  5.2369e-03]]),\n",
       " tensor([-0.0817, -0.3687, -0.0597, -0.0567,  0.1577, -0.1665, -0.1026, -0.4500,\n",
       "          0.1803, -0.1153])]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[par.grad for par in l2.parameters()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[tensor([[ 0.0062,  0.0037, -0.0688, -0.0557,  0.0636, -0.1009,  0.1864,  0.0177,\n",
       "          -0.0253,  0.0334],\n",
       "         [ 0.0052, -0.0037,  0.0979,  0.0644, -0.0530,  0.0902, -0.1534, -0.0197,\n",
       "           0.0321, -0.0113],\n",
       "         [ 0.0226,  0.0143,  0.0461, -0.0201, -0.0022,  0.0261, -0.0684,  0.0159,\n",
       "           0.0367,  0.0099],\n",
       "         [-0.0083, -0.0115, -0.0684, -0.0103,  0.0534, -0.1003,  0.2102, -0.0090,\n",
       "          -0.0474,  0.0249],\n",
       "         [-0.0287, -0.0193, -0.0998, -0.0123,  0.0222, -0.0641,  0.1401, -0.0080,\n",
       "          -0.0510, -0.0153],\n",
       "         [ 0.0070,  0.0216, -0.0420, -0.0555,  0.0205, -0.0293,  0.0103,  0.0281,\n",
       "          -0.0042,  0.0086],\n",
       "         [ 0.0042, -0.0068,  0.0112,  0.0198,  0.0185, -0.0343,  0.0794, -0.0090,\n",
       "          -0.0113,  0.0293],\n",
       "         [ 0.0235,  0.0347, -0.0149, -0.0380,  0.0503, -0.0694,  0.0686,  0.0190,\n",
       "          -0.0160,  0.0510],\n",
       "         [ 0.0125,  0.0026,  0.0496,  0.0236, -0.0075,  0.0127, -0.0192, -0.0066,\n",
       "           0.0096,  0.0162],\n",
       "         [ 0.0124,  0.0227, -0.0080, -0.0126,  0.0269, -0.0413,  0.0495,  0.0015,\n",
       "          -0.0200,  0.0229]]),\n",
       " tensor([ 0.2477, -0.2150, -0.0208,  0.2017,  0.1185,  0.0822,  0.0592,  0.1319,\n",
       "         -0.0343,  0.0679])]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[par.grad for par in l1.parameters()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "l1.zero_grad()\n",
    "l2.zero_grad()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]),\n",
       "  tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])],\n",
       " [tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "          [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]),\n",
       "  tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[par.grad for par in l2.parameters()], [par.grad for par in l1.parameters()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "g_loss = loss1(x, y)\n",
    "g_loss.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[tensor([[-2.7389e-02,  1.1402e-02, -1.3026e-02, -1.9048e-02,  2.6334e-02,\n",
       "          -4.4004e-02, -1.1785e-02, -3.4722e-02,  2.0086e-02, -3.4167e-02],\n",
       "         [-9.5504e-02,  7.0130e-02, -1.5741e-02, -1.0722e-01,  4.3244e-02,\n",
       "          -1.4641e-01,  1.1110e-02, -1.0949e-01,  1.1689e-01, -7.9042e-02],\n",
       "         [-1.1131e-02,  1.6760e-02, -8.1567e-03,  2.1239e-02,  1.5258e-02,\n",
       "          -2.7346e-02,  2.2119e-02, -2.6933e-02,  2.1116e-02,  8.4700e-05],\n",
       "         [-8.4151e-03,  1.3514e-02,  6.6931e-03, -3.5969e-02, -1.5061e-02,\n",
       "          -1.4408e-02,  3.7014e-03, -1.4785e-02,  2.3280e-02,  8.8669e-03],\n",
       "         [ 5.0170e-02, -1.8824e-02,  6.6798e-03,  7.7978e-02,  3.5121e-03,\n",
       "           3.5753e-02,  6.5429e-03,  3.0482e-02, -5.1790e-02,  1.4147e-02],\n",
       "         [-2.6445e-02,  4.6301e-02,  4.2772e-02, -8.4189e-02, -3.3793e-02,\n",
       "          -7.6141e-02,  1.6340e-02, -4.9749e-02,  6.0687e-02, -5.9759e-03],\n",
       "         [-2.7532e-02,  1.7192e-02,  1.7555e-02, -6.6511e-02, -2.3261e-02,\n",
       "          -3.1535e-02, -2.2324e-03, -2.0739e-02,  3.4416e-02, -2.8350e-03],\n",
       "         [-1.0294e-01,  1.0090e-01,  3.4701e-02, -1.3869e-01, -2.0929e-04,\n",
       "          -1.9544e-01,  4.0068e-02, -1.3714e-01,  1.4739e-01, -6.2956e-02],\n",
       "         [ 4.1102e-02, -4.1929e-02,  4.8118e-03,  1.1362e-02, -2.2476e-02,\n",
       "           7.5625e-02, -3.4561e-02,  5.8165e-02, -5.9864e-02,  2.4102e-02],\n",
       "         [-2.3423e-02,  2.5135e-02,  3.3604e-02, -9.1833e-02, -4.1923e-02,\n",
       "          -2.3969e-02,  7.2757e-03, -8.3311e-03,  4.7152e-02,  5.2369e-03]]),\n",
       " tensor([-0.0817, -0.3687, -0.0597, -0.0567,  0.1577, -0.1665, -0.1026, -0.4500,\n",
       "          0.1803, -0.1153])]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[par.grad for par in g_l2.parameters()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[tensor([[-0.0062, -0.0037,  0.0688,  0.0557, -0.0636,  0.1009, -0.1864, -0.0177,\n",
       "           0.0253, -0.0334],\n",
       "         [-0.0052,  0.0037, -0.0979, -0.0644,  0.0530, -0.0902,  0.1534,  0.0197,\n",
       "          -0.0321,  0.0113],\n",
       "         [-0.0226, -0.0143, -0.0461,  0.0201,  0.0022, -0.0261,  0.0684, -0.0159,\n",
       "          -0.0367, -0.0099],\n",
       "         [ 0.0083,  0.0115,  0.0684,  0.0103, -0.0534,  0.1003, -0.2102,  0.0090,\n",
       "           0.0474, -0.0249],\n",
       "         [ 0.0287,  0.0193,  0.0998,  0.0123, -0.0222,  0.0641, -0.1401,  0.0080,\n",
       "           0.0510,  0.0153],\n",
       "         [-0.0070, -0.0216,  0.0420,  0.0555, -0.0205,  0.0293, -0.0103, -0.0281,\n",
       "           0.0042, -0.0086],\n",
       "         [-0.0042,  0.0068, -0.0112, -0.0198, -0.0185,  0.0343, -0.0794,  0.0090,\n",
       "           0.0113, -0.0293],\n",
       "         [-0.0235, -0.0347,  0.0149,  0.0380, -0.0503,  0.0694, -0.0686, -0.0190,\n",
       "           0.0160, -0.0510],\n",
       "         [-0.0125, -0.0026, -0.0496, -0.0236,  0.0075, -0.0127,  0.0192,  0.0066,\n",
       "          -0.0096, -0.0162],\n",
       "         [-0.0124, -0.0227,  0.0080,  0.0126, -0.0269,  0.0413, -0.0495, -0.0015,\n",
       "           0.0200, -0.0229]]),\n",
       " tensor([-0.2477,  0.2150,  0.0208, -0.2017, -0.1185, -0.0822, -0.0592, -0.1319,\n",
       "          0.0343, -0.0679])]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[par.grad for par in l1.parameters()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor(1.2589, grad_fn=<MseLossBackward>),\n",
       " tensor(1.2589, grad_fn=<MseLossBackward>))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g_loss, n_loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
